import java.util.Arrays;

public class Mystack implements IStack{
    private int[] elem;
    private int usedsize;
    private static final int DEFAULT_SIZE=10;
    public Mystack(){
        elem=new int[DEFAULT_SIZE];
    }

    @Override
    public void push(int x) {
        if (full()){
            elem= Arrays.copyOf(elem,elem.length*2);
        }
        elem[usedsize]=x;
        usedsize++;
    }

    @Override
    public int pop() {
        if (empty()){
            //抛异常
            throw new StackEcpetion("栈为空！");
        }
        int old=elem[usedsize-1];
        usedsize--;
        return old;
    }

    @Override
    public int peek() {
        if (empty()){
            //抛异常
            throw new StackEcpetion("栈为空！");
        }
        int old=elem[usedsize-1];
        return old;
    }

    @Override
    public int size() {
        return usedsize;
    }

    @Override
    public boolean empty() {
        return usedsize==0;
    }

    @Override
    public boolean full() {
        return usedsize==elem.length;
    }

    public int leafSize=0;
    int getLeafNodeCount(TreeNode root){
        if(root.left==null&&root.right==null){
            leafSize++;
        }
        getLeafNodeCount(root.left);
        getLeafNodeCount(root.right);
    }
    int getLeafNodeCount2(TreeNode root){
        if(root.left==null&&root.right==null){
            return 0;
        }
        return getLeafNodeCount2(root.left)+getLeafNodeCount2(root.right);

    }
}
